<?php

namespace App\ZhCN;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use App\Tools\Auxi;
use Phoenix\Log\Log4p as logger;

/**
 * 首页
 */
class Index {

    private function __Controller() {}

    private function __Value($__PACKAGE__, $__ROOT__, $__RM__, $setting, $cfg, $__LANGUAGE_ID__) {}

    protected function __Inject($session, Service\Archives $servArc, Repository\Archives $repoArc,
                                Repository\Category $category) {}

    public function index() {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__);
            //解决方案
            $caseId = $this->__LANGUAGE_ID__ == 1 ? 25 : 13;
            $_model['solveList'] = $this->repoArc->findHomeArchives($caseId, 0, 4, true);
            //新闻动态
            $newsId = $this->__LANGUAGE_ID__ == 1 ? 27 : 17;
            $_model['newsList'] = $this->repoArc->findHomeArchives($newsId, 0, 4, true);
            return array (
                'model' => $_model,
                'view' => true
            );
        }
        return 404;
    }

    public function category($__Route = array('/*/{aliasId}/{page:\d*}', '/category/{aliasId}/{page}')) {
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
            $_model['aliasId'] = $this->aliasId;
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;

            if (!isset($this->page)) {
                $this->page = 1;
            }
            $_model['currentPage'] = $this->page;

            $_model['footerLink'] = $this->repoArc->getFooterLink($_model['categoryId']);

            //根目录下的总条数
            $_model['rootCategoryTotal'] = $this->servArc->findAllRootCount($_model['rootId']);
//            if (!$_model['isPart'] && $_model['currentPage'] > ceil($_model['rootCategoryTotal'] / $_model['currentPageSize'])) {
//                return 404;
//            }
            if ($_model['categoryLevel'] == 1 && in_array($_model['channelType'], array(5))) { //列表
                $_model['currentCategoryTotal'] = $_model['rootCategoryTotal'];
                $_model['currentListRs'] = $this->servArc->findAllRoot($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            } else {
                $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            }
            //核心团队轮播
            $_model['teamAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 1);

            //解决方案
            $caseId = $this->__LANGUAGE_ID__ == 1 ? 25 : 13;
            $_model['solveList'] = $this->repoArc->findHomeArchives($caseId, 0, 4, true);

            $_model['contactHash'] = md5(time() . 'zy_contact_hash');
            $this->session->contactHash = array('hash' => $_model['contactHash'], 'time' => 0);

            return array(
                'model' => $_model,
                'view' =>  $this->setting['aryChannelTypeMapping'][$this->__PACKAGE__][$_model['channelType']][1]
            );
        }
        return 404;
    }

    public function detail($__Route = array('/*/{aliasId}/details/{id:\d+}')) {
        $_GET['view'] = $this->aliasId;
        if (!is_null(($_model = $this->servArc->getSubstance($this->id)))) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $_model['src'] = $this->repoArc->findArchivesSrc($this->id);
            //浏览次数加一
            $this->repoArc->viewCount($this->id);
            $_model['aliasId'] = $_GET['aliasId'];
            $_model['contactHash'] = md5(time() . 'zy_contact_hash');
            $this->session->contactHash = array('hash' => $_model['contactHash'], 'time' => 0);

            //解决方案
            $caseId = $this->__LANGUAGE_ID__ == 1 ? 25 : 13;
            $_model['solveList'] = $this->repoArc->findHomeArchives($caseId, 0, 4, true);
            return array(
                'model' => $_model,
                'view' => $this->aliasId . '/details'
            );
        }
        return 404;
    }

    /**
     * @Title: detail1
     * @param array $__Route
     * @Description: TODO 解决方案详情页
     * @return array|int
     * @author TUGE
     * @date
     */
//    public function detail1($__Route = array('/*/{aliasId}/details/{id:\d+}')) {
//        $_GET['view'] = $this->aliasId;
//        if (!is_null(($_model = $this->servArc->getSubstance($this->id)))) {
//            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
//            $_model['src'] = $this->repoArc->findArchivesSrc($this->id);
//            $_model['aliasId'] = $_GET['aliasId'];
//            return array(
//                'model' => $_model,
//                'view' => $this->aliasId . '/details'
//            );
//        }
//        return 404;
//    }

}
